<?php

namespace App\Repositories;

use App\Exceptions\BasicException;

class OpenBannerRepository
{
    /**
     * @Desc:二级开屏列表
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/1 15:17
     */
    public function list($param)
    {
        $time = time();
        $list = app('model_open_banner')
            ->where(function ($query) use ($param, $time) {
            if (isset($param['type'])) {
                $query->where('type', $param['type']);
            }
            if (isset($param['status'])) {
                switch ($param['status']){
                    case 1:
                        $query->where('start_time', '>', $time);
                        break;
                    case 2:
                        $query->where('start_time', '<=', $time)->where('end_time', '>=', $time);
                        break;
                    case 3:
                        $query->where('end_time', '<', $time);
                        break;
                }
            }
            if (isset($param['title']) && $param['title']) {
                $query->where('title', 'like','%'.$param['title'].'%');
            }
        })->orderBy('id', 'desc')
            ->paginate($param['page_size'] ?? 10, ['*'], '', $param['page'] ?? 1);
        //需求:生效中status=2只展示一个,其他的不展示
        $openId = app('model_open_banner')->where('start_time', '<=', $time)->where('end_time', '>=', $time)->orderBy('id', 'desc')->value('id');
        foreach ($list as &$item) {
            //根据时间判断
            if ($time < $item->start_time){
                $item->status = 1;
            } elseif ($time >= $item->start_time && $time <= $item->end_time){
                if ($item->id == $openId){
                    $item->status = 2;
                } else {
                    $item->status = 3;
                }
            } else {
                $item->status = 3;
            }
            $item->start_time = date('Y-m-d H:i:s',$item->start_time);
            $item->end_time = date('Y-m-d H:i:s',$item->end_time);
        }
        return $list;
    }


    /**
     * @Desc:新增
     * @param $params
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function create($params)
    {
        if ($params['type'] == 2 && empty($params['url'])){
            throw new BasicException(0, '请添加链接地址');
        }
        $data = [
            'title' => $params['title'],
            'type' => $params['type'],
            'image' => $params['image'],
            'url' => $params['url'] ?? '',
            'start_time' => strtotime($params['start_time']),
            'end_time' => strtotime($params['end_time']),
        ];
        if (!empty($params['room_id'])){
            $roomId = app('model_user_room')->where('room_id', $params['room_id'])->value('id');
            if (empty($roomId)){
                throw new BasicException(0, '房间不存在!');
            }
            $data['room_id'] = $roomId;
        }
        $res = app('model_open_banner')->create($data);
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '添加失败');
        }
    }

    /**
     * @Desc:详情
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function info($param)
    {
        $info = app('model_open_banner')
            ->where('id', $param['id'])
            ->first();
        if (!empty($info['room_id'])){
            $roomId = app('model_user_room')->where('id', $info['room_id'])->value('room_id');
            $info['room_id'] = $roomId ?? '';
        }
        $info['start_time'] = date('Y-m-d H:i:s', $info['start_time']);
        $info['end_time'] = date('Y-m-d H:i:s', $info['end_time']);
        return $info;
    }

    /**
     * @Desc:更新
     * @param $params
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function update($params)
    {
        $id = $params['id'];
        if ($params['type'] == 2 && empty($params['url'])){
            throw new BasicException(0, '请添加链接地址');
        }
        $data = [
            'title' => $params['title'],
            'type' => $params['type'],
            'image' => $params['image'],
            'url' => $params['url'] ?? '',
            'start_time' => strtotime($params['start_time']),
            'end_time' => strtotime($params['end_time']),
        ];
        if (!empty($params['room_id'])){
            $roomId = app('model_user_room')->where('room_id', $params['room_id'])->value('id');
            if (empty($roomId)){
                throw new BasicException(0, '房间不存在!');
            }
            $data['room_id'] = $roomId;
        }
        $res = app('model_open_banner')->where('id', $id)->update($data);
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '编辑失败');
        }
    }

    /**
     * @Desc:删除
     * @param $param
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function delete($param)
    {
        $res = app('model_open_banner')->where('id', $param['id'])->delete();
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '删除失败');
        }
    }
}
